home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / rdflib / sparql / sparqlOperators.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  12.6 KB  |  421 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import sys
  5. import os
  6. import re
  7. from rdflib.Literal import Literal
  8. from rdflib.BNode import BNode
  9. from rdflib.URIRef import URIRef
  10. from rdflib.sparql.graphPattern import _createResource
  11. from rdflib.sparql import _questChar, Debug
  12. from rdflib.sparql.Unbound import Unbound
  13.  
  14. def queryString(v):
  15.     if isinstance(v, basestring) and len(v) != 0:
  16.         pass
  17.     return v[0] == _questChar
  18.  
  19.  
  20. def getLiteralValue(v):
  21.     return v
  22.  
  23.  
  24. def getValue(param):
  25.     
  26.     def f(bindings):
  27.         if unBound:
  28.             val = bindings[param]
  29.             if isinstance(val, Literal):
  30.                 return getLiteralValue(val)
  31.             return val
  32.         unBound
  33.         return value
  34.  
  35.     return f
  36.  
  37.  
  38. def lt(a, b):
  39.     fa = getValue(a)
  40.     fb = getValue(b)
  41.     
  42.     def f(bindings):
  43.         
  44.         try:
  45.             return fa(bindings) < fb(bindings)
  46.         except:
  47.             if Debug:
  48.                 (typ, val, traceback) = sys.exc_info()
  49.                 sys.excepthook(typ, val, traceback)
  50.             
  51.             return False
  52.  
  53.  
  54.     return f
  55.  
  56.  
  57. def le(a, b):
  58.     fa = getValue(a)
  59.     fb = getValue(b)
  60.     
  61.     def f(bindings):
  62.         
  63.         try:
  64.             return fa(bindings) <= fb(bindings)
  65.         except:
  66.             if Debug:
  67.                 (typ, val, traceback) = sys.exc_info()
  68.                 sys.excepthook(typ, val, traceback)
  69.             
  70.             return False
  71.  
  72.  
  73.     return f
  74.  
  75.  
  76. def gt(a, b):
  77.     fa = getValue(a)
  78.     fb = getValue(b)
  79.     
  80.     def f(bindings):
  81.         
  82.         try:
  83.             return fa(bindings) > fb(bindings)
  84.         except:
  85.             if Debug:
  86.                 (typ, val, traceback) = sys.exc_info()
  87.                 sys.excepthook(typ, val, traceback)
  88.             
  89.             return False
  90.  
  91.  
  92.     return f
  93.  
  94.  
  95. def ge(a, b):
  96.     fa = getValue(a)
  97.     fb = getValue(b)
  98.     
  99.     def f(bindings):
  100.         
  101.         try:
  102.             return fa(bindings) >= fb(bindings)
  103.         except:
  104.             if Debug:
  105.                 (typ, val, traceback) = sys.exc_info()
  106.                 sys.excepthook(typ, val, traceback)
  107.             
  108.             return False
  109.  
  110.  
  111.     return f
  112.  
  113.  
  114. def eq(a, b):
  115.     fa = getValue(a)
  116.     fb = getValue(b)
  117.     
  118.     def f(bindings):
  119.         
  120.         try:
  121.             return fa(bindings) == fb(bindings)
  122.         except:
  123.             if Debug:
  124.                 (typ, val, traceback) = sys.exc_info()
  125.                 sys.excepthook(typ, val, traceback)
  126.             
  127.             return False
  128.  
  129.  
  130.     return f
  131.  
  132.  
  133. def neq(a, b):
  134.     fa = getValue(a)
  135.     fb = getValue(b)
  136.     
  137.     def f(bindings):
  138.         
  139.         try:
  140.             return fa(bindings) != fb(bindings)
  141.         except:
  142.             if Debug:
  143.                 (typ, val, traceback) = sys.exc_info()
  144.                 sys.excepthook(typ, val, traceback)
  145.             
  146.             return False
  147.  
  148.  
  149.     return f
  150.  
  151.  
  152. def __getQueryString(v):
  153.     if isinstance(v, Unbound):
  154.         return v.name
  155.     if queryString(v):
  156.         return v
  157.     return None
  158.  
  159.  
  160. def bound(a):
  161.     v = __getQueryString(a)
  162.     
  163.     def f(bindings):
  164.         if v == None:
  165.             return False
  166.         if v in bindings:
  167.             val = bindings[v]
  168.             return not (val == None)
  169.         return False
  170.  
  171.     return f
  172.  
  173.  
  174. def isURI(a):
  175.     v = __getQueryString(a)
  176.     
  177.     def f(bindings):
  178.         if v == None:
  179.             return False
  180.         
  181.         try:
  182.             val = bindings[v]
  183.             if val == None:
  184.                 return False
  185.             return isinstance(val, URIRef)
  186.         except:
  187.             v == None
  188.             return False
  189.  
  190.  
  191.     return f
  192.  
  193.  
  194. def isIRI(a):
  195.     return isURI(a)
  196.  
  197.  
  198. def isBlank(a):
  199.     v = __getQueryString(a)
  200.     
  201.     def f(bindings):
  202.         if v == None:
  203.             return False
  204.         
  205.         try:
  206.             val = bindings[v]
  207.             if val == None:
  208.                 return False
  209.             return isinstance(val, BNode)
  210.         except:
  211.             v == None
  212.             return False
  213.  
  214.  
  215.     return f
  216.  
  217.  
  218. def isLiteral(a):
  219.     v = __getQueryString(a)
  220.     
  221.     def f(bindings):
  222.         if v == None:
  223.             return False
  224.         
  225.         try:
  226.             val = bindings[v]
  227.             if val == None:
  228.                 return False
  229.             return isinstance(val, Literal)
  230.         except:
  231.             v == None
  232.             return False
  233.  
  234.  
  235.     return f
  236.  
  237.  
  238. def str(a):
  239.     v = __getQueryString(a)
  240.     
  241.     def f(bindings):
  242.         if v == None:
  243.             return ''
  244.         
  245.         try:
  246.             val = bindings[v]
  247.             if val == None:
  248.                 return ''
  249.             return `val`
  250.         except:
  251.             v == None
  252.             return ''
  253.  
  254.  
  255.     return f
  256.  
  257.  
  258. def lang(a):
  259.     v = __getQueryString(a)
  260.     
  261.     def f(bindings):
  262.         if v == None:
  263.             return ''
  264.         
  265.         try:
  266.             val = bindings[v]
  267.             if val == None:
  268.                 return ''
  269.             return val.lang
  270.         except:
  271.             v == None
  272.             return ''
  273.  
  274.  
  275.     return f
  276.  
  277.  
  278. def datatype(a):
  279.     v = __getQueryString(a)
  280.     
  281.     def f(bindings):
  282.         if v == None:
  283.             if isinstance(a, Literal):
  284.                 return a.datatype
  285.             return ''
  286.         v == None
  287.         
  288.         try:
  289.             val = bindings[v]
  290.             if val == None:
  291.                 return ''
  292.             return val.datatype
  293.         except:
  294.             return ''
  295.  
  296.  
  297.     return f
  298.  
  299.  
  300. def isOnCollection(collection, item, triplets):
  301.     """Generate a method that can be used as a global constaint in sparql to check whether
  302.     the 'item' is an element of the 'collection' (a.k.a. list). Both collection and item can
  303.     be a real resource or a query string. Furthermore, item might be a plain string, that is
  304.     then turned into a literal run-time.
  305.     The method returns an adapted method.
  306.     """
  307.     collUnbound = False
  308.     if isinstance(collection, Unbound):
  309.         collUnbound = True
  310.         collection = collection.name
  311.     elif queryString(collection):
  312.         collUnbound = True
  313.     else:
  314.         collUnbound = False
  315.     if isinstance(item, Unbound):
  316.         queryItem = item.name
  317.         itUnbund = True
  318.     elif queryString(item):
  319.         queryItem = item
  320.         itUnbound = True
  321.     else:
  322.         queryItem = _createResource(item)
  323.         itUnbound = False
  324.     
  325.     def checkCollection(bindings):
  326.         
  327.         try:
  328.             if collUnbound == True:
  329.                 coll = bindings[collection]
  330.             else:
  331.                 coll = collection
  332.             if itUnbound == True:
  333.                 it = bindings[queryItem]
  334.             else:
  335.                 it = queryItem
  336.             return it in triplets.items(coll)
  337.         except:
  338.             return True
  339.  
  340.  
  341.     return checkCollection
  342.  
  343.  
  344. def addOperator(args, combinationArg):
  345.     '''
  346.     SPARQL numeric + operator implemented via Python
  347.     '''
  348.     return []([ 'sparqlOperators.getValue(%s)%s' % (i, '') for i in args ])
  349.  
  350.  
  351. def XSDCast(source, target = None):
  352.     """
  353.     XSD Casting/Construction Support
  354.     For now (this may be an issue since Literal doesn't override comparisons) it simply creates
  355.     a Literal with the target datatype using the 'lexical' value of the source
  356.     """
  357.     sFunc = getValue(source)
  358.     
  359.     def f(bindings):
  360.         rt = sFunc(bindings)
  361.         if isinstance(rt, Literal) and rt.datatype == target:
  362.             return rt
  363.         return Literal(rt, datatype = target)
  364.  
  365.     return f
  366.  
  367.  
  368. def regex(item, pattern, flag = None):
  369.     '''
  370.     Invokes the XPath fn:matches function to match text against a regular expression pattern.
  371.     The regular expression language is defined in XQuery 1.0 and XPath 2.0 Functions and Operators section 7.6.1 Regular Expression Syntax
  372.     '''
  373.     a = getValue(item)
  374.     b = getValue(pattern)
  375.     if flag:
  376.         cFlag = 0
  377.         usedFlags = []
  378.         for fChar, _flag in [
  379.             ('i', re.IGNORECASE),
  380.             ('s', re.DOTALL),
  381.             ('m', re.MULTILINE)]:
  382.             if fChar in flag and fChar not in usedFlags:
  383.                 cFlag |= _flag
  384.                 usedFlags.append(fChar)
  385.                 continue
  386.         
  387.         
  388.         def f1(bindings):
  389.             
  390.             try:
  391.                 return bool(re.compile(b(bindings), cFlag).search(a(bindings)))
  392.             except:
  393.                 return False
  394.  
  395.  
  396.         return f1
  397.     
  398.     def f2(bindings):
  399.         
  400.         try:
  401.             return bool(re.compile(b(bindings)).search(a(bindings)))
  402.         except:
  403.             return False
  404.  
  405.  
  406.     return f2
  407.     
  408.     def f(bindings):
  409.         
  410.         try:
  411.             print '%s %s' % (a(bindings), b(bindings))
  412.             return bool(re.compile(a(bindings)).search(b(bindings)))
  413.         except Exception:
  414.             e = None
  415.             print e
  416.             return False
  417.  
  418.  
  419.     return f
  420.  
  421.